﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using Microsoft.DirectX;

namespace NGE
{
    public class SceneComponent : Component
    {
        public SceneComponent(Game g)
            : base(g)
        {
            IsShowExtendTile = true;
            menuBattleAct = new MenuBattleAct(game);
        }

        MenuBattleAct menuBattleAct;

        public bool IsShowExtendTile;

        public void OnPlayerCharacterActCode(object sender, PlayerCharacterEventArgs ea)
        {
            if (ea.Act == PlayerCharacterActs.ChangeDirection)
            {
                Datagram.PlayerCharacterMoveInfo pcmi = (Datagram.PlayerCharacterMoveInfo)ea.Arg;
                if (game.IsOnline)
                {
                    pcmi.Steps = 0;
                    game.ClientCOM.UploadPCMoveInfo(pcmi);
                }
            }
            if (ea.Act == PlayerCharacterActs.StartMove)
            {
                Datagram.PlayerCharacterMoveInfo pcmi = (Datagram.PlayerCharacterMoveInfo)ea.Arg;
                game.SCN.Move(pcmi.Direction, pcmi.Steps, pcmi.MoveSpeed);
                if (game.IsOnline)
                {
                    game.ClientCOM.UploadPCMoveInfo(pcmi);
                }
            }
            if (ea.Act == PlayerCharacterActs.StopMove)
            {
                if (game.IsOnline)
                {
                    Datagram.PlayerCharacterSceneInfo pcsi = (Datagram.PlayerCharacterSceneInfo)ea.Arg;
                    game.ClientCOM.UploadPCSceneInfo(pcsi);
                }
            }
            if (ea.Act == PlayerCharacterActs.EnterScene)
            {
                //game.ClientCOM.UploadExitScene(game.PC.Name);

                Datagram.PlayerCharacterSceneInfo csi = (Datagram.PlayerCharacterSceneInfo)ea.Arg;

                Controllable = false;

                EnterScene(game.LoadScene(csi.InSceneName));
                SetCHR(game.PC, csi.Location, csi.Direction);
                game.PC.InSceneName = csi.InSceneName;

                Controllable = true;

                if (game.IsOnline)
                {
                    game.ClientCOM.UploadPC(game.PC);
                    game.ClientCOM.RequestOPCs();
                }
            }
        }

        public void EnterScene(Scene scn)
        {
            game.ClearNPCs();
            game.ClearDoors();

            if (game.SCN != null)
            {
                game.SCN.Dispose();
            }
            game.Graph.FreeVideoMemory();
            if (game.Sound.Load(game.Option.MusicPath + scn.MusicFileName))
            {
                game.Sound.IsLoop = true;
                game.Sound.Play();
            }

            if (scn.NPCs == null)
            {
                scn.NPCs = new List<Character>();
            }
            foreach (Character npc in scn.NPCs)
            {
                npc.Location = npc.DefaultLocation;
                npc.RestoreDirection();
                npc.RestoreDoorState();
                game.CHRLogin(npc);
                //npc.AvatarSize = game.SCN.TileSize;
            }

            if (game.SCN != null)
                game.SCN.NPCs = null;

            //if (scn.OPCs == null)
            //{
            //    scn.OPCs = new List<PlayerCharacter>();
            //}

            game.SCN = scn;
        }
        public void SetCHR(Character pc, Vector3 chrLoc, Direction chrDir)
        {
            if (pc == null)
            {
                return;
            }
            pc.ClearCommandQueue();
            pc.InSceneName = game.SCN.Name;
            pc.MountEventHandler(OnPlayerCharacterActCode);
            //pc.OnPlayerCharacterAct += new PlayerCharacterEvent(OnPlayerCharacterActCode);
            if (pc.DrivingMetal != null)
            {
                pc.DrivingMetal.InSceneName = game.SCN.Name;
            }
            pc.Direction = chrDir;
            SetCHR(pc, chrLoc);
        }
        void SetCHR(Character pc, Vector3 loc)
        {
            if (game.SCN == null)
            {
                return;
            }
            if (pc == null)
            {
                return;
            }
            Vector3 cp = new Microsoft.DirectX.Vector3(game.Option.WindowSize.Width / game.SCN.TileSize.Width / 2 / (int)game.Option.Zoom, game.Option.WindowSize.Height / game.SCN.TileSize.Height / 2 / (int)game.Option.Zoom, 0);
            pc.JumpTo(loc, game.SCN.TileSize);
            if (pc.Type == SpriteType.PC)
            {
                game.SCN.JumpTo(loc, cp);
            }
        }

        protected int frameIndex = 0;

        protected void drawSCN(Scene scn,  List<Character> chrs)
        {
            if (scn == null)
            {
                return;
            }
            if (IsShowExtendTile)
            {
                #region extend tile
                if (scn.ExtendTile != null)
                {
                    for (int y = -scn.TileSize.Height; y <= game.Option.WindowSize.Height / game.Option.Zoom; y += scn.TileSize.Height)
                    {
                        //left
                        for (int x = scn.TileSize.Width; x <= scn.Location.X + scn.TileSize.Width; x += scn.TileSize.Width)
                        {
                            Vector3 v3 = new Vector3(-x, y, 0);

                            v3.X += scn.Location.X;
                            v3.Y += scn.Location.Y % scn.TileSize.Height;

                            int i = frameIndex % scn.ExtendTile.Frames.Count;

                            v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                            game.Graph.DrawImage(scn.TileImageSet[scn.ExtendTile.Frames[i].TextureIndexer.Index], FilterColor, v3);
                        }
                        //right
                        int xmax = (int)((game.Option.WindowSize.Width / game.Option.Zoom) - (scn.Location.X + scn.Size.Width * scn.TileSize.Width));
                        for (int x = 0; x <= xmax; x += scn.TileSize.Width)
                        {
                            Vector3 v3 = new Vector3(x, y, 0);
                            v3.X += scn.Location.X;
                            v3.Y += scn.Location.Y % scn.TileSize.Height;

                            v3.X += scn.Size.Width * scn.TileSize.Width;

                            int i = frameIndex % scn.ExtendTile.Frames.Count;
                            v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                            game.Graph.DrawImage(scn.TileImageSet[scn.ExtendTile.Frames[i].TextureIndexer.Index], FilterColor, v3);
                        }
                    }
                    for (int x = 0; x < scn.Size.Width * scn.TileSize.Width; x += scn.TileSize.Width)
                    {
                        //top
                        for (int y = 0; y < scn.Location.Y; y += scn.TileSize.Height)
                        {
                            Vector3 v3 = new Vector3(x, -y, 0);
                            v3 = Util.Vector3AddVector3(v3, scn.Location);

                            v3.Y -= scn.TileSize.Height;

                            int i = frameIndex % scn.ExtendTile.Frames.Count;
                            v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                            game.Graph.DrawImage(scn.TileImageSet[scn.ExtendTile.Frames[i].TextureIndexer.Index], FilterColor, v3);

                        }
                        //bottom
                        int ymax = (int)(game.Option.WindowSize.Height / game.Option.Zoom - (scn.Location.Y + scn.Size.Height * scn.TileSize.Height));
                        for (int y = 0; y <= ymax; y += scn.TileSize.Height)
                        {
                            Vector3 v3 = new Vector3(x, y, 0);
                            v3 = Util.Vector3AddVector3(v3, scn.Location);

                            v3.Y += scn.Size.Height * scn.TileSize.Height;

                            int i = frameIndex % scn.ExtendTile.Frames.Count;
                            v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                            game.Graph.DrawImage(scn.TileImageSet[scn.ExtendTile.Frames[i].TextureIndexer.Index], FilterColor, v3);
                        }
                    }
                }
                #endregion
            }
            int layerIndex = 0;
            foreach (Terrain.Layer layer in game.SCN.Terrain.Layers)
            {
                if (layer.Visible)
                {
                    if (game.PC != null)
                    {
                        if (layerIndex == 2)
                        {
                            drawNPCs(scn, game.CHRs);
                            drawDoors(scn, game.CHRs);
                        }
                        foreach (Terrain.Layer.Tile tile in layer.Tiles)
                        {
                            Vector3 v3 = Util.Vector3AddVector3(scn.Location, tile.Location);
                            if (v3.X < 0 - scn.TileSize.Width || v3.X > game.Option.WindowSize.Width / game.Option.Zoom || v3.Y < 0 - scn.TileSize.Height || v3.Y > game.Option.WindowSize.Height / game.Option.Zoom)
                            {
                                continue;
                            }
                            if (tile.Location.Y <= game.PC.Location.Y)
                            {
                                int i = frameIndex % tile.Frames.Count;
                                if (i > -1)
                                {
                                    i = tile.Frames[i].TextureIndexer.Index;
                                    if (i < scn.TileImageSet.Count)
                                    {
                                        v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                                        game.Graph.DrawImage(scn.TileImageSet[i], FilterColor, v3);
                                    }
                                }
                            }
                        }
                        if (layerIndex == 3)
                        {
                            //drawSpritesMoveZone(scn, pc, opcs);
                            //drawSpritesAttackZone(scn, pc, opcs);
                            drawMTLs(scn, game.PC);
                            drawSprite(scn, game.PC);
                            drawNPCs(scn, game.CHRs);
                            //drawOPCs(scn, opcs);
                            drawDoors(scn, game.CHRs);
                        }
                        foreach (Terrain.Layer.Tile tile in layer.Tiles)
                        {
                            Vector3 v3 = Util.Vector3AddVector3(scn.Location, tile.Location);
                            if (v3.X < 0 - scn.TileSize.Width || v3.X > game.Option.WindowSize.Width / game.Option.Zoom || v3.Y < 0 - scn.TileSize.Height || v3.Y > game.Option.WindowSize.Height / game.Option.Zoom)
                            {
                                continue;
                            }
                            if (tile.Location.Y > game.PC.Location.Y)
                            {
                                int i = frameIndex % tile.Frames.Count;
                                if (i > -1)
                                {
                                    i = tile.Frames[i].TextureIndexer.Index;
                                    if (i < scn.TileImageSet.Count)
                                    {
                                        v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                                        game.Graph.DrawImage(scn.TileImageSet[i], FilterColor, v3);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (layerIndex == 2)
                        {
                            drawNPCs(scn, game.CHRs);
                            drawDoors(scn, game.CHRs);
                        }
                        foreach (Terrain.Layer.Tile tile in layer.Tiles)
                        {
                            Vector3 v3 = Util.Vector3AddVector3(scn.Location, tile.Location);
                            if (v3.X < 0 - scn.TileSize.Width || v3.X > game.Option.WindowSize.Width / game.Option.Zoom || v3.Y < 0 - scn.TileSize.Height || v3.Y > game.Option.WindowSize.Height / game.Option.Zoom)
                            {
                                continue;
                            }
                            //if (tile.Location.Y <= chr.Location.Y)
                            {
                                int i = frameIndex % tile.Frames.Count;
                                if (i > -1)
                                {
                                    i = tile.Frames[i].TextureIndexer.Index;
                                    if (i < scn.TileImageSet.Count)
                                    {
                                        v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                                        game.Graph.DrawImage(scn.TileImageSet[i], FilterColor, v3);
                                    }
                                }
                            }
                        }
                    }
                }
                layerIndex++;
            }
        }

        void drawSprite(Scene scn, Character chr)
        {
            if (chr == null)
            {
                return;
            }

            //drawMoveZone(scn, chr);
            //drawAttackZone(scn, chr);

            Vector3 v3 = Util.Vector3AddVector3(scn.Location, chr.Location);
            v3.Y -= chr.AvatarSize.Height - scn.TileSize.Height;
            Rectangle rect = new Rectangle();

            if (chr.InMetalIndex<0)
            {
                rect.X = chr.GetAvatarFrame(scn.TileSize) * chr.AvatarSize.Width;
                rect.Y = (int)chr.Direction * chr.AvatarSize.Height;
                rect.Size = chr.AvatarSize;

                v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                game.Graph.DrawImage(chr.Avatar, FilterColor, rect, v3);
            }
            else
            {
                rect.X = chr.GetAvatarFrame(scn.TileSize) * chr.DrivingMetal.AvatarSize.Width;
                rect.Y = (int)chr.Direction * chr.DrivingMetal.AvatarSize.Height;
                rect.Size = chr.DrivingMetal.AvatarSize;

                v3.X += 4;
                v3.Y -= 4;

                v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                game.Graph.DrawImage(chr.DrivingMetal.Avatar, FilterColor, rect, v3);
            }

            //v3.Y -= 16;
            //game.Graph.DrawText(chr.Name, Color.Blue, v3);
            //if (game.RunningTotalSeconds - chr.SaidStartTime < chr.SaidDuration)
            //{
            //    v3.Y -= 32;
            //    v3.X -= 64;
            //    game.Graph.DrawText(chr.Said, Color.Blue, v3);
            //}
        }
        void drawDoor(Scene scn, Character door)
        {
            if (door == null)
            {
                return;
            }

            Vector3 v3 = Util.Vector3AddVector3(scn.Location, door.Location);
            v3.Y -= door.AvatarSize.Height - scn.TileSize.Height;
            Rectangle rect = new Rectangle();

            int i = (int)door.DoorState;
            rect.X = i * scn.TileSize.Width * door.AvatarSize.Width;
            rect.Y = 0;
            rect.Size = door.AvatarSize;

            v3 = Util.Vector3AddVector3(v3, Vector3Offset);
            game.Graph.DrawImage(door.Avatar, FilterColor, rect, v3);
        }

        void drawNPCs(Scene scn, List<Character> npcs)
        {
            if (npcs == null)
            {
                return;
            }
            foreach (Character npc in npcs)
            {
                if (npc.Type == SpriteType.NPC)
                {
                    drawSprite(scn, npc);
                }

            }
        }
        void drawDoors(Scene scn, List<Character> npcs)
        {
            if (npcs == null)
            {
                return;
            }
            foreach (Character npc in npcs)
            {
                if (npc.Type == SpriteType.Door)
                {
                    drawDoor(scn, npc);
                }
            }
        }

        void drawMTLs(Scene scn, Character chr)
        {
            if (chr == null)
            {
                return;
            }

            for (int i = 0; i < chr.MetalCount; i++)
            {
                Metal mtl = chr.GetMetal(i);
                if (mtl.InSceneName == scn.Name)
                {
                    if (mtl.IsRunning == false)
                    {
                        Vector3 v3 = Util.Vector3AddVector3(scn.Location, mtl.StopLocation);
                        //loc.Y -= chr.AvatarSize.Height - scn.TileSize.Height;
                        Rectangle rect = new Rectangle();

                        //rect.X = 0;
                        rect.Y = (int)mtl.StopDirection * mtl.AvatarSize.Height;
                        rect.Size = mtl.AvatarSize;
                        if (mtl.AvatarSize.Width > scn.TileSize.Width)
                        {
                            v3.X += 4;
                        }
                        if (mtl.AvatarSize.Height > scn.TileSize.Height)
                        {
                            v3.Y -= 4;
                        }

                        v3 = Util.Vector3AddVector3(v3, Vector3Offset);
                        game.Graph.DrawImage(mtl.Avatar, FilterColor, rect, v3);
                    }
                }
            }
        }

        //void drawAttackZone(Scene scn, PlayerCharacter chr)
        //{
        //    if (chr.BattleAct != PlayerCharacterActs.BattleAttackCommanding)
        //    {
        //        return;
        //    }

        //    foreach (Vector3 v3 in chr.CanAttackZone)
        //    {
        //        Vector3 v33 = v3;
        //        v33.X *= scn.TileSize.Width;
        //        v33.Y *= scn.TileSize.Height;
        //        v33 = Util.Vector3AddVector3(v33, scn.Location);
        //        v33 = Util.Vector3AddVector3(v33, chr.Location);
        //        Point loc = Util.Vector32Point(v33);
        //        game.Graph.DrawRectangle(Color.FromArgb(160, 255, 0, 0), new Rectangle(loc, scn.TileSize), true);
        //    }

        //    Vector3 v333 = Util.Vector3AddVector3(chr.TargetLocation, scn.Location);
        //    Rectangle rect = new Rectangle();
        //    rect.X = (int)v333.X;
        //    rect.Y = (int)v333.Y;
        //    rect.Size = scn.TileSize;
        //    game.Graph.DrawRectangle(Color.FromArgb(160, 0, 255, 0), rect, true);
        //}
        //void drawMoveZone(Scene scn, PlayerCharacter chr)
        //{
        //    if (chr.BattleAct != PlayerCharacterActs.BattleMoveCommanding)
        //    {
        //        return;
        //    }

        //    foreach (Vector3 v3 in chr.CanMoveZone)
        //    {
        //        Vector3 v33 = v3;
        //        v33.X *= scn.TileSize.Width;
        //        v33.Y *= scn.TileSize.Height;
        //        v33 = Util.Vector3AddVector3(v33, scn.Location);
        //        v33 = Util.Vector3AddVector3(v33, chr.Location);
        //        Point loc = Util.Vector32Point(v33);
        //        game.Graph.DrawRectangle(Color.FromArgb(160, 0, 0, 255), new Rectangle(loc, scn.TileSize), true);
        //    }

        //    Vector3 v333 = Util.Vector3AddVector3(chr.TargetLocation, scn.Location);
        //    Rectangle rect = new Rectangle();
        //    rect.X = (int)v333.X;
        //    rect.Y = (int)v333.Y;
        //    rect.Size = scn.TileSize;
        //    game.Graph.DrawRectangle(Color.FromArgb(160, 0, 255, 0), rect, true);
        //}

        //void drawSpritesAttackZone(Scene scn, PlayerCharacter pc, List<PlayerCharacter> opcs)
        //{
        //    if (game.SCN != null)
        //    {
        //        if (game.PC != null)
        //        {
        //            drawAttackZone(scn, pc);
        //        }

        //        if (game.OPCs != null)
        //        {
        //            foreach (PlayerCharacter opc in opcs)
        //            {
        //                drawAttackZone(scn, opc);
        //            }
        //        }

        //        if (game.SCN.NPCs != null)
        //        {
        //            foreach (NonPlayerCharacter npc in scn.NPCs)
        //            {
        //                drawAttackZone(scn, npc);
        //            }
        //        }
        //    }
        //}
        //void drawSpritesMoveZone(Scene scn, PlayerCharacter pc, List<PlayerCharacter> opcs)
        //{
        //    if (game.SCN != null)
        //    {
        //        if (game.PC != null)
        //        {
        //            drawMoveZone(scn, pc);
        //        }

        //        if (game.OPCs != null)
        //        {
        //            foreach (PlayerCharacter opc in opcs)
        //            {
        //                drawMoveZone(scn, opc);
        //            }
        //        }

        //        if (game.SCN.NPCs != null)
        //        {
        //            foreach (NonPlayerCharacter npc in scn.NPCs)
        //            {
        //                drawMoveZone(scn, npc);
        //            }
        //        }
        //    }
        //}


        public override void OnGamepadEventCode(object sender, int btn, GamepadState gs)
        {
            //if (game.SCN == null)
            //{
            //    return;
            //}
            //if (game.PC == null)
            //{
            //    return;
            //}
            //if ((int)sender == 0)
            //{
            //    if (gs == GamepadState.ButtonHold)
            //    {
            //        if (btn == Xinput.XINPUT_GAMEPAD_DPAD_UP)
            //        {
            //            game.PC.Move(Direction.U, 1, game.SCN);
            //        }
            //        else if (btn == Xinput.XINPUT_GAMEPAD_DPAD_LEFT)
            //        {
            //            game.PC.Move(Direction.L, 1, game.SCN);
            //        }
            //        else if (btn == Xinput.XINPUT_GAMEPAD_DPAD_DOWN)
            //        {
            //            game.PC.Move(Direction.D, 1, game.SCN);
            //        }
            //        else if (btn == Xinput.XINPUT_GAMEPAD_DPAD_RIGHT)
            //        {
            //            game.PC.Move(Direction.R, 1, game.SCN);
            //        }
            //    }
            //}
        }

        public override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (game.SCN == null)
            {
                return;
            }

            //if (game.SCN.IsFighting)
            //{
            //    return;
            //}
            if (game.GUICOM.IsBusy)
            {
                return;
            }
            if (ks == KeyboardState.KeyUp)
            {
                if (key == game.Option.KeyA)
                {
                    TalkToNPC();
                    //game.Script.ES("shock 2000");
                    //game.Script.ES("delay 2000");
                    //game.Script.ES("fallout 1000");
                    //game.Script.ES("delay 1000");
                    //game.Script.ES("fallin 1000");
                    //game.Script.ES("delay 1000");
                }
                else if (key == game.Option.KeyB)
                {
                    //game.Script.AS("npc-00 move u 2");
                    //game.Script.AS("npc-00 move d 2");
                    //game.Script.ES("sound data\\music\\003.mp3");
                }
            }
            else if (ks == KeyboardState.KeyHold)
            {
                if (key == game.Option.KeyU || key == game.Option.KeyL || key == game.Option.KeyD || key == game.Option.KeyR)
                {
                    if (game.PC.NeedMove > 0)
                    {
                        return;
                    }
                    int steps = 1;
                    Direction dir = Direction.U;
                    if (key == game.Option.KeyU)
                    {
                        dir = Direction.U;
                    }
                    else if (key == game.Option.KeyL)
                    {
                        dir = Direction.L;
                    }
                    else if (key == game.Option.KeyD)
                    {
                        dir = Direction.D;
                    }
                    else if (key == game.Option.KeyR)
                    {
                        dir = Direction.R;
                    }
                    game.PC.Move(dir, steps, game.SCN, game.CHRs);
                }
            }
        }

        public override void Heartbeat()
        {
            if (game.SCN == null)
            {
                return;
            }
            timer.Record();
            if (timer.Seconds > game.SCN.FrameInterval / 1000.0)
            {
                frameIndex++;
                timer.Reset();
            }

            if (game.SCN == null)
            {
                return;
            }
            game.SCN.Heartbeat();

            for (int i = 0; i < game.CHRs.Count; i++)
            {
                game.CHRs[i].Heartbeat(game);
            }

            //for (int i = 0; i < game.OPCs.Count; i++)
            //{
            //    if (game.OPCs[i].InSceneName != game.SCN.Name)
            //    {
            //        game.OPCs.RemoveAt(i);
            //    }
            //    else
            //    {
            //        game.OPCs[i].Heartbeat(game.SCN, true);
            //    }
            //}

            //BattleHeartbeat();
        }

        public override void Show()
        {
            if (game.SCN == null)
            {
                return;
            }
            drawSCN(game.SCN, game.CHRs);
        }

        //void BattleHeartbeat()
        //{
        //    if (game.SCN.IsFighting == false)
        //    {
        //        return;
        //    }
        //    if (actQ == null)
        //    {
        //        actQ = new Queue<int>();
        //    }
        //    if (actQ.Count == 0)
        //    {
        //        int i = 0;
        //        foreach (NonPlayerCharacter npc in game.SCN.NPCs)
        //        {
        //            if (npc.IsEnemy)
        //            {
        //                actQ.Enqueue(i);
        //                i++;
        //            }
        //        }
        //        actQ.Enqueue(-1);
        //    }


        //    int index = actQ.Peek();
        //    if (index > -1)
        //    {
        //        NonPlayerCharacter npc = game.SCN.NPCs[index];
        //        npc.IsWaitingCommand = true;
        //        if (npc.BattleDone)
        //        {
        //            foreach (NonPlayerCharacter n in game.SCN.NPCs)
        //            {
        //                if (n.BattleDone == false)
        //                {
        //                    return;
        //                }
        //            }
        //            npc.BattleHeartbeat();
        //        }
        //        else
        //        {
        //            npc.IsWaitingCommand = false;
        //            NextOne();
        //        }
        //    }
        //    else
        //    {
        //        foreach (NonPlayerCharacter n in game.SCN.NPCs)
        //        {
        //            if (n.BattleDone == false)
        //            {
        //                return;
        //            }
        //        }
        //        if (game.PC.IsWaitingCommand == false)
        //        {
        //            game.OpenPanelbox(menuBattleAct, null);
        //        }
        //    }
        //}
        //public void OnNonPlayerCharacterActCode(object sender, PlayerCharacterEventArgs ea)
        //{ 
        //}
        //Queue<int> actQ;
        //public void NextOne()
        //{                    
        //    actQ.Dequeue();
        //}


        public void TalkToNPC()
        {
            if (game.SCN == null)
            {
                return;
            }
            Character npc = game.GetCHR(game.PC.GetNextLocation(game.SCN.TileSize));
            if (!game.PC.CanMove(game.SCN))
            {
                if (npc == null)
                {
                    npc = game.GetCHR(game.PC.GetNextNextLocation(game.SCN.TileSize));
                }
            }
            if (npc != null)
            {
                if (npc.Scrpit == null)
                {
                    return;
                }
                if (npc.Scrpit == string.Empty)
                {
                    return;
                }
                //freeze();
                
                npc.Direction = Util.GetOppositeDirection(game.PC.Direction);
                game.Script.AS("clrctrl\n" + npc.Scrpit + "\n" + npc.Name + " rdir\nsetctrl");
                //game.OpenPanelbox("msgbox.ngui", str);
            }
        }

        //public void TalkOver()
        //{
        //    Character npc = game.GetCHR(game.PC.GetNextLocation(game.SCN.TileSize));
        //    if (npc != null)
        //    {
        //        npc.RestoreDirection();
        //    }
        //    game.ClosePanelbox();
        //}
    }
}
